<?php
if(!defined('CORE_ROOT')) exit();
require CORE_ROOT.'include/admin.inc.php';
require CORE_ROOT.'include/app.func.php';
require CORE_ROOT.'include/task.file.func.php';
checkcreator();
if($get_action == 'appshop') {
	$html = '';
	$xml = readfromurl('http://app.akcms.com/api/list.xml');
	if(empty($xml)) {
		$html .= $lan['connapplierror'];
	} else {
		if(strpos($xml, '<?xml') === false) aexit();
		$apiresult = xml2array($xml);
		if(!empty($apiresult) && is_array($apiresult)) {
			$apps = $apiresult['apps']['app'];
			$installedapps = $db->querytoarray("SELECT `key` FROM {$tablepre}_apps");
			$currentversion = $db->get_by('value', 'settings', "variable='dataversion'");
			$tmparray = array();
			foreach($installedapps as $iv) {
				$tmparray[] = $iv['key'];
			}
			foreach($apps as $v) {
				$price = $v['price'].$lan['yuan'];
				if($v['price'] == 0) $price = $lan['free'];
				$icon = $v['icon'];
				$installkey = $v['key'];
				if(!empty($v['dependapp'])) $installkey .= ",{$v['dependapp']}";
				$isinstall = "<a href='index.php?file=app&action=install&key=$installkey'>{$lan['install']}</a>";
				if(in_array($v['key'], $tmparray)) {
					$isinstall = "<span style='color:#666666;'>{$lan['isinstall']}</span>";
				} elseif($v['dependence'] > $currentversion) {
					$isinstall = "<span style='color:#666666;' title='AKCMS{$v['dependence']}{$lan['appversiontip']}'>{$lan['nosupport']}</span>";
				}
				if(empty($v['icon'])) $icon = 'http://s.akcms.com/images/app-default-icon.png';
				$html .= "<div class='app'>
				<div class='appicon'><a href='http://app.akcms.com/app.php?key={$v['key']}' target='_blank'><img src='{$icon}' width=64 height=64 title='{$v['introduce']}' /></a></div>
				<ul class='appdetail'>
					<li class='appname'>{$v['name']}</li>
					<li class='price'>{$price}</li>
					<li class='productor'>{$lan['kernelrequire']}:{$v['dependence']}</li>
				</ul>
				<div class='appbotton'>
				<div class='installbotton'>{$isinstall}</div><div class='detailbotton'><a href='http://app.akcms.com/app.php?key={$v['key']}' target='_blank'>{$lan['detail']}</a></div>
				</div>
				</div>";
			}
		} else {
			$html .= $lan['getappllierr'];
		}
	}
	displaytemplate('admincp_app.htm', array('html' => $html, 'apppagename' => $lan['appshop']));
} elseif($get_action == 'install') {
	$keys = explode(',', $get_key);
	foreach($keys as $key) {
		$result = downloadapp($key);
		if($result === false) {
			$error = 1;
			break;
		}
	}
	if(!empty($error)) adminmsg($lan['installapperror'], '', 0, 1);
	scanapps();
	updatecache('templateplugins');
	adminmsg($lan['operatesuccess'], 'index.php?file=app&action=installed');
} elseif($get_action == 'installed') {
	updatecache('apps');
	$html = '';
	$alreadyinstalled = $db->query_by('*', 'apps');
	while($v = $db->fetch_array($alreadyinstalled)) {
		$installtime = date('y-m-d', $v['updatetime']);
		$html .= "<div class='app'>
			<div class='appicon'><a href='index.php?app={$v['key']}'><img src='{$v['picture']}' /></a></div>
			<ul class='appdetail'>
				<li class='appname'>{$v['app']}</li>
				<li class='productor'>{$lan['version']}:{$v['ver']}</li>
			</ul>
			<div class='appbotton'>
				<div class='installbotton'><a class='uninstall' href='#'>{$lan['appuninstall']}</a></div><div class='detailbotton'><a href='http://app.akcms.com/app.php?key={$v['key']}' target='_blank'>{$lan['detail']}</a></div>
			</div>
			<div class='ajaxkey' style='display:none;'>{$v['key']}</div>
		</div>";
	}
	if(empty($html)) $html="{$lan['noappinstalled']}";
	displaytemplate('admincp_installedapp.htm', array('html' => $html, 'apppagename' => $lan['alreadyinstalled']));
} elseif($get_action == 'uninstall') {
	$uninstallpath = CORE_ROOT.'configs/apps/'.$get_key;
	ak_rmdir($uninstallpath);
	updatecache('apps');
} elseif($get_action == 'refresh') {
	updatecache('apps');
	header('location:index.php?file=app&action=installed');
	aexit();
}
runinfo();
aexit();
?>